package com.tianhui.consignortwofei.mvp.ui.activity.audit.vehicle;

import android.os.Bundle;
import android.text.TextUtils;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonView;
import com.fgs.common.entity.PictureInfo;
import com.fgs.common.widget.itemView.InputItemView;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.mvp.model.enty.SelectPhotoInfo;
import com.tianhui.consignortwofei.mvp.model.enty.VehicleInfo;
import com.tianhui.consignortwofei.mvp.model.enty.authLicense.VehicleLicenseInfo;
import com.tianhui.consignortwofei.other.auth.PictureContext;
import com.tianhui.consignortwofei.other.auth.inte.SelectPhotoCallback;
import com.tianhui.consignortwofei.other.auth.stragety.VehicleBackStrategy;

import butterknife.BindView;
import butterknife.OnClick;

public class VehicleTowedBackAuditActivity extends BaseVehicleAuditActivity {

    @BindView(R.id.activity_vehicle_towed_back_audit_plateNumberInputItemView)
    InputItemView mPlateNumberInputItemView;
    @BindView(R.id.activity_vehicle_towed_back_audit_lengthInputItemView)
    InputItemView mLengthNumberInputItemView;
    @BindView(R.id.activity_vehicle_towed_back_audit_widthInputItemView)
    InputItemView mWidthNumberInputItemView;
    @BindView(R.id.activity_vehicle_towed_back_audit_heightInputItemView)
    InputItemView mHeightNumberInputItemView;
    private String mBackTowedImageId;

    @Override
    public int getLayoutId() {
        return R.layout.activity_vehicle_towed_back_audit;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }

    @Override
    public String getToolbarSubtitle() {
        return "跳过";
    }

    @Override
    public void onSubtitleClick() {
        openActivityForResult(VehicleRoadAuditActivity.class, getIntent().getExtras());
    }

    @Override
    public void changePictureSuccess(PictureInfo pictureInfo) {

        mBackTowedImageId = pictureInfo.id;

    }

    @Override
    public void showVehicleInfo(VehicleInfo info) {

        mPlateNumberInputItemView.setContent(info.railerehicletvplatenumber);
        mLengthNumberInputItemView.setContent(info.length);
        mWidthNumberInputItemView.setContent(info.width);
        mHeightNumberInputItemView.setContent(info.height);
        loadPicture(info.imgdrivingendimagecopy);

        mBackTowedImageId = info.drivingendimagecopy;
    }

    @Override
    public VehicleInfo getSaveInfo() {
        String length = mLengthNumberInputItemView.getContent();
        String width = mWidthNumberInputItemView.getContent();
        String height = mHeightNumberInputItemView.getContent();
        if (TextUtils.isEmpty(length)) {
            showToast("请输入长度");
            return null;
        }
        if (TextUtils.isEmpty(width)) {
            showToast("请输入宽度");
            return null;
        }
        if (TextUtils.isEmpty(height)) {
            showToast("请输入高度");
            return null;
        }
        mVehicleInfo.length = length;
        mVehicleInfo.width = width;
        mVehicleInfo.height = height;
        mVehicleInfo.drivingendimagecopy = mBackTowedImageId;
        return mVehicleInfo;
    }

    @Override
    public void nextAudit() {

        VehicleInfo vehicleInfo = getSaveInfo();
        if (vehicleInfo != null) {
            Bundle bundle = new Bundle();
            bundle.putParcelable(KeyValue.KEY_VEHICLE_INFO, vehicleInfo);
            bundle.putInt(KeyValue.KEY_OPERATE, getIntent().getExtras().getInt(KeyValue.KEY_OPERATE));
            openActivityForResult(VehicleRoadAuditActivity.class, bundle);
        }

    }

    @OnClick(R.id.layout_audit_picture_editImageView)
    public void changePicture() {
        VehicleBackStrategy vehicleBackStrategy = (VehicleBackStrategy) pictureStrategyFactory.createStrategy(VehicleBackStrategy.class);
        pictureContext = new PictureContext(vehicleBackStrategy);
        pictureContext.selectPhoto(getContext(), new SelectPhotoCallback<VehicleLicenseInfo>() {
            @Override
            public void selectPhotoSuccess(SelectPhotoInfo<VehicleLicenseInfo> resultInfo) {
                mBackTowedImageId = resultInfo.pictureId;
                loadPicture(resultInfo.realFileName);
                processVehicleTowedBackResult(resultInfo.resultInfo);
            }

            @Override
            public void selectPhotoFailed() {

            }
        });
    }

    private void processVehicleTowedBackResult(VehicleLicenseInfo resultInfo) {
        if (resultInfo == null) return;
        mLengthNumberInputItemView.setContent(resultInfo.length);
        mWidthNumberInputItemView.setContent(resultInfo.width);
        mHeightNumberInputItemView.setContent(resultInfo.height);
    }
}
